<?php
class Gmapsprofile_AdminSettingsController extends Core_Controller_Action_Admin {
	
	public function indexAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'gmapsprofile_admin_main', array (), 'gmapsprofile_admin_main_settings' );
		
		$settings = Engine_Api::_ ()->getApi ( 'settings', 'core' );
		$this->view->form = $form = new Gmapsprofile_Form_Admin_Settings ();
		$metaData = Engine_Api::_ ()->getApi ( 'core', 'fields' )->getFieldsMeta ( 'user' );
		$optionsData = Engine_Api::_ ()->getApi ( 'core', 'fields' )->getFieldsOptions ( 'user' );
		$profile_type = $metaData->getRowMatching ( 'type', 'profile_type' );
		$LevelOption = $optionsData->getRowsMatching ( 'field_id', $profile_type->field_id );
		foreach ( $LevelOption as $option ) {
			$level = 'gmapsprofile_level_' . $option->option_id;
			$block = $form->getElement ( 'level_' . $option->option_id );
			if (isset ( $block ))
				$form->getElement ( 'level_' . $option->option_id )->setValue ( isset ( $settings->$level ) ? explode ( ';', $settings->$level ) : '' );
		}
		
		$form->getElement ( 'scale_profile' )->setValue ( isset ( $settings->gmapsprofile_scale_profile ) ? $settings->gmapsprofile_scale_profile : 16 );
		$form->getElement ( 'type_map' )->setValue ( isset ( $settings->gmapsprofile_type_map ) ? $settings->gmapsprofile_type_map : 'ROADMAP' );
		$form->getElement ( 'scale_friends' )->setValue ( isset ( $settings->gmapsprofile_scale_friends ) ? $settings->gmapsprofile_scale_friends : 2 );
		$form->getElement ( 'type_map_friends' )->setValue ( isset ( $settings->gmapsprofile_tm_friends ) ? $settings->gmapsprofile_tm_friends : 'ROADMAP' );
		
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			
			foreach ( $this->getRequest ()->getPost () as $sItem => $aValue ) {
				if (strstr ( $sItem, 'level_' )) {
					$aField = $this->getRequest ()->getPost ( $sItem );
					$iFieldID = ( int ) str_replace ( 'level_', '', $sItem );
					$level = 'gmapsprofile_level_' . $iFieldID;
					$value = implode ( ';', $aField );
					if (empty ( $value ))
						$value = '';
					$settings->$level = $value;
				}
			}
			$settings->gmapsprofile_scale_profile = $this->_getParam ( 'scale_profile', 16 );
			$settings->gmapsprofile_type_map = $this->_getParam ( 'type_map', 1 );
			$settings->gmapsprofile_scale_friends = $this->_getParam ( 'scale_friends', 2 );
			$settings->gmapsprofile_tm_friends = $this->_getParam ( 'type_map_friends', 1 );
			$form->addNotice ( 'Your changes have been saved.' );
		}
	}
	
	public function levelAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'gmapsprofile_admin_main', array (), 'gmapsprofile_admin_main_level' );
		
		// Get level id
		if (null !== ($id = $this->_getParam ( 'level_id' )))
			$level = Engine_Api::_ ()->getItem ( 'authorization_level', $id );
		else
			$level = Engine_Api::_ ()->getItemTable ( 'authorization_level' )->getDefaultLevel ();
		
		if (! $level instanceof Authorization_Model_Level)
			throw new Engine_Exception ( 'missing level' );
		
		$id = $level->level_id;
		
		// Make form
		$this->view->form = $form = new Gmapsprofile_Form_Admin_Level ();
		$form->level_id->setValue ( $id );
		
		$permissionsTable = Engine_Api::_ ()->getDbtable ( 'permissions', 'authorization' );
		
		// Check post
		if (! $this->getRequest ()->isPost ()) {
			$form->populate ( $permissionsTable->getAllowed ( 'gmapsprofile', $id, array_keys ( $form->getValues () ) ) );
			return;
		}
		
		// Check validitiy
		if (! $form->isValid ( $this->getRequest ()->getPost () ))
			return;
		
		// Process
		$values = $form->getValues ();
		$values = array ('viewmy' => $values ['viewmy'], 'viewfriend' => $values ['viewfriend'] );
		
		$db = $permissionsTable->getAdapter ();
		$db->beginTransaction ();
		
		try {
			// Set permissions
			$permissionsTable->setAllowed ( 'gmapsprofile', $id, $values );
			
			// Commit
			$db->commit ();
			
			$form->addNotice ( 'Save ok' );
		} 

		catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
	
	}
	
	//More Plugins
	public function moreAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'gmapsprofile_admin_main', array (), 'gmapsprofile_admin_main_more' );
	}
}